<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card shadow="hover">
      <!-- 添加分类 按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 主体区域 -->
      <el-table :data="cateList" row-key="cat_id" border>
        <!-- <el-table-column type="index" label="编号"> </el-table-column> -->
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
        <el-table-column label="是否有效">
          <template v-slot="scope">
            <i
              class="el-icon-success"
              style="color: lightgreen"
              v-if="!scope.row.cat_deleted"
            ></i>
            <i class="el-icon-error" style="color: red" v-else></i>
          </template>
        </el-table-column>
        <el-table-column label="排序">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="primary"
              v-if="scope.row.cat_level === 0"
              >一级</el-button
            >
            <el-button
              size="mini"
              type="success"
              v-else-if="scope.row.cat_level === 1"
              >二级</el-button
            >
            <el-button size="mini" type="warning" v-else>三级</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryInfo.pagenum"
        :page-sizes="[5, 10, 20]"
        :page-size="this.queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类 对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <!-- 添加分类 表单 -->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            size="5"
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChanged"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        pagenum: 1,
        pagesize: 5,
      },
      cateList: [],
      total: 0,
      // 控制 添加分类 对话框的显示与隐藏
      addCateDialogVisible: false,
      // 添加分类 表单数据 对象
      addCateForm: {
        // 分类名称
        cat_name: '',
        // 父级分类ID，默认 0 对应一级分类·
        cat_pid: 0,
        // 分类等级，默认一级分类
        cat_level: 0,
      },
      // 添加分类 表单验证规则 对象
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
        ],
      },
      // 父级分类 列表
      parentCateList: [],
      //  指定 级联选择器 配置对象
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true,
      },
      // 选中的 父级分类 的ID数组
      selectedKeys: [],
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.cateList = res.data.result
      this.total = res.data.total
    },

    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getCateList()
    },

    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getCateList()
    },

    // 点击按钮，展示 添加分类 对话框
    showAddCateDialog() {
      this.getParentCateList()

      this.addCateDialogVisible = true
    },

    // 获取 父级分类数据 列表
    async getParentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 },
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取父级分类数据列表失败！')
      this.parentCateList = res.data
    },

    // 级联选择器 选项变化 触发
    parentCateChanged() {
      // console.log(this.selectedKeys)

      // 选中一个 父级分类
      if (this.selectedKeys.length > 0) {
        // 分类父ID
        this.addCateForm.cat_pid = this.selectedKeys[
          this.selectedKeys.length - 1
        ]
        // 分类等级
        this.addCateForm.cat_level = this.selectedKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },

    // 添加新的分类
    addCate() {
      this.$refs.addCateFormRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post(
          'categories',
          this.addCateForm
        )
        if (res.meta.status !== 201)
          return this.$message.error('添加分类失败！')
      })
      this.$message.success('添加分类成功！')
      this.handleCurrentChange(1)
      this.addCateDialogVisible = false
    },

    // 监听添加分类对话框关闭事件
    addCateDialogClosed() {
      // 重置表单数据
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
  },
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>